package com.gilight.base.util;

import com.alibaba.fastjson.JSON;
import com.gilight.base.base.JwtUserToken;
import com.gilight.base.base.LoginUserInfo;
import com.gilight.base.config.jwt.common.Constant;
import com.gilight.base.config.jwt.util.JwtUtil;
import com.gilight.base.model.SysUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;

/**
 * Shiro工具类
 *
 * @author wuzz
 * @date 15:10 2019/5/13
 */
public class ShiroUtil {

    /**
     * 获取登录用户ID
     *
     * @return
     * @Author: wuzz
     * @Date 2019/5/13 15:22
     */
    public static SysUser getUser() {
//        SysUser user = new SysUser();
//        LoginUserInfo loginUser = (LoginUserInfo)SecurityUtils.getSubject().getPrincipal();
//        if(loginUser!=null) {
//            BeanUtils.copyProperties(loginUser, user);
//        }
//        return user;
        JwtUserToken sysUser = JSON.parseObject(
                JwtUtil.getClaim( String.valueOf(SecurityUtils.getSubject().getPrincipal()), Constant.ACCOUNT)
                , JwtUserToken.class);
        SysUser sysUser1 = new SysUser();
        BeanUtils.copyProperties(sysUser,sysUser1);
        return sysUser1;
    }

    public static SysUser getJWTUser(){
        JwtUserToken sysUser = JSON.parseObject(
                JwtUtil.getClaim( String.valueOf(SecurityUtils.getSubject().getPrincipal()), Constant.ACCOUNT)
                , JwtUserToken.class);
        SysUser sysUser1 = new SysUser();
        BeanUtils.copyProperties(sysUser,sysUser1);
        return sysUser1;
    }

}
